<?php
/* wwwschool project

Copyright (C) 2009  Phillip Aldridge
Email : info@imi21.com
Web site http://www.imi21.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/


/**
 *  File : editContent.php
 *  called only via ajax 
 *  
 *  requires POST  
 */  


checkSession();
if ($session->userType() > 3) // admin, head master & teacher
{
  die('Wrong user rights');
  exit;
}

require '../../includes/classes/WS3formview.php';
require '../../includes/classes/WS3module.php';
require '../../includes/classes/WS3content.php';

$date=date("Y-m-d H:i:s");



// are we inserting a new content ?

if ($id=='-1') 
{
  // insert new content at the last postion+1
  $module = new WS3_Module();
  if ( $module->setModuleID( $_POST['module_id']) )
  {// module exists
    $position=0;
    $db=new WS3_Database();
    $q="SELECT MAX(position)  AS p FROM content WHERE 1 AND module_id= '".$module->ModuleID()."' LIMIT 1";
  
    if ( $db->query($q) && $db->nextRecord() )
    {
      $position=$db->f('p')+1;
    }
    $q="INSERT INTO content (id,date_created,module_id,position )
    VALUES
    (NULL,'$date','".$module->ModuleID()."','$position')";
    $db->query($q); 
    $id = $db->insertID();  
  }
}


$content = new WS3_Content();


if ( $content->setContentID( $id ) )
{ // content does exisits
  $message='';
  
  $module = new WS3_Module();
  $module->setModuleID( $content->ModuleID());


  $form = new WS3_FormView('main'); 
  $form->setTitle( 'Edit content : '. $content->ContentName()."  in module ". $module->ModuleName()  ); //

  $form->displayResetButton( true );
  $form->setAction( 'update' );
  $form->addHiddenField( 'f', 'Content' ); // which function to call
  $form->addHiddenField( 'function2', 'list' ); // which function to call after update
  $form->addHiddenField( 'param1', 'contents' ); // parameter1 for post
  $form->addHiddenField( 'module_id', $module->ModuleID() );
  $form->addHiddenField( 'id', $content->ContentID() );
  
  $form->addFieldSet( 'info', 'Information');
  $form->addTextField(  'info','Name','name','name',$content->ContentName() );
  $form->addTextArea(  'info','Description','description','description', $content->ContentDescription() );
  
  $form->addFieldSet( 'options', 'Options');
  $form->addSelect( 'options','Scoring', 'scoring_type', 'scoring_type', $WS3_SCORE_TYPE, $content->ContentScoringType() );
  $form->addSelect( 'options','Access', 'access_type', 'access_type', $WS3_ACCESS_TYPE, $content->ContentAccessType() );
  $form->addSelect( 'options','Media', 'media_type', 'media_type', $WS3_MEDIA_TYPE, $content->ContentMediaType() );  
  
  
  //$form->addTextField( 'Media type','media_type','media_type', $content->ContentMediaType() );
  //$form->addTextField( 'Access type','access_type','access_type', $content->ContentAccessType() ); 
  echo $form->display();
  
  $message="Editing";
  }else{
  $message="Error : ID not found";
}


/** Get menu */
require '../../includes/menus/topContents.php';
$menu=javaSafeString($menu);
echo "document.getElementById('topmenu').innerHTML='$menu';";

echo "document.getElementById('messages').innerHTML='$message';";
if (W3DEBUG) $debug=new WS3_Debug();
?>
